/**
 * @author hdeom
 * @date 2014. 1. 17.
 *
*/

package h.d.bluetooth.visitor;

import h.d.bluetooth.visitor.common.VaE;
import android.content.ContentResolver;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;

/**
 * @author hdeom
 * @date 2014. 1. 17.
 *
 */
public class Setup extends PreferenceActivity implements OnPreferenceChangeListener {
	public static final String TAG = "Setup";

	private CheckBoxPreference pScan_mode;
	private CheckBoxPreference pBattery_mode;
	private EditTextPreference pScan_edit;
	private EditTextPreference pBattery_edit;
	private CheckBoxPreference pVibration;
	private CheckBoxPreference pPopup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.setup);

		pScan_mode = (CheckBoxPreference) findPreference("scan_mode");
		pBattery_mode = (CheckBoxPreference) findPreference("battery_mode");

		pScan_edit = (EditTextPreference) findPreference("scan_edit");
		pBattery_edit = (EditTextPreference) findPreference("battery_edit");

		pVibration = (CheckBoxPreference) findPreference("vibration");
		pPopup = (CheckBoxPreference) findPreference("popup");

		pScan_mode.setOnPreferenceChangeListener(this);
		pBattery_mode.setOnPreferenceChangeListener(this);

		pScan_edit.setOnPreferenceChangeListener(this);
		pBattery_edit.setOnPreferenceChangeListener(this);

		pVibration.setOnPreferenceChangeListener(this);
		pPopup.setOnPreferenceChangeListener(this);

	}


	@Override
	public void onContentChanged() {
		super.onContentChanged();
		if(VaE.HD) Log.d(TAG, "onContentChanged");
	}


	@Override
	protected void onResume() {
		super.onResume();
		updateUi();
	}


	/**
	 * @author hdeom
	 * @date 2014. 1. 22.
	 * @param
	 * @return void
	 *
	**/

	private void updateUi() {

		//if(VaE.HD) Log.d(TAG, "scan_edit.getText(); "+ scan_edit.getText());

		pScan_mode.setChecked(TextUtils.equals(App.getPreferenceValue(App.SCAN_MODE, "y"), "y") ? true : false);
		pBattery_mode.setChecked(TextUtils.equals(App.getPreferenceValue(App.LOW_BETTERY, "y"), "y") ? true : false);

		pScan_edit.setText(App.getPreferenceValue(App.SCAN_CYCLE, "5"));
		pBattery_edit.setText(App.getPreferenceValue(App.LOW_BETTERY_VALUE, "30"));

		pVibration.setChecked(TextUtils.equals(App.getPreferenceValue(App.VIBRATION, "y"), "y") ? true : false);
		pPopup.setChecked(TextUtils.equals(App.getPreferenceValue(App.POPUP, "y"), "y") ? true : false);
	}



	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if(VaE.HD) Log.d(TAG, "onKeyDown");
		return super.onKeyDown(keyCode, event);
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(VaE.HD) Log.d(TAG, "onDestroy");
	}

	@Override
	public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
		super.setPreferenceScreen(preferenceScreen);
		if(VaE.HD) Log.d(TAG, "setPreferenceScreen");
	}


	@Override
	public boolean onPreferenceChange(Preference arg0, Object arg1) {

		if(VaE.HD) Log.d(TAG, "onPreferenceChange  " + arg0.getKey() + " object = "+arg1);

		//updateUi();

		if(TextUtils.equals(arg0.getKey(), "scan_mode")){

			App.setPreferenceValue(App.SCAN_MODE, ((Boolean) arg1 ? "y" : "n"));

		}else if(TextUtils.equals(arg0.getKey(), "scan_edit")){

			App.setPreferenceValue(App.SCAN_CYCLE, (String.valueOf(arg1)));

		}else if(TextUtils.equals(arg0.getKey(), "battery_mode")){

			App.setPreferenceValue(App.LOW_BETTERY, ((Boolean) arg1 ? "y" : "n"));

		}else if(TextUtils.equals(arg0.getKey(), "battery_edit")){

			App.setPreferenceValue(App.LOW_BETTERY_VALUE,  (String.valueOf(arg1)));

		}else if(TextUtils.equals(arg0.getKey(), "vibration")){

			App.setPreferenceValue(App.VIBRATION,  ((Boolean) arg1 ? "y" : "n"));

		}else if(TextUtils.equals(arg0.getKey(), "popup")){

			App.setPreferenceValue(App.POPUP,  ((Boolean) arg1 ? "y" : "n"));
		}

		updateUi();

		return true;
	}



}
